package com.atguigu.es;

import com.atguigu.beans.Employee;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

public class WriteSingleDoc1 {
    public static void main(String[] args) throws IOException {
        //一.创建和es数据库的连接,获取一个客户端

        //1. new出这个工厂
        JestClientFactory factory = new JestClientFactory();
        //2. 看源码这个工厂有连接es地址的默认属性,得主动为属性赋值,否则连不上
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        factory.setHttpClientConfig(httpClientConfig);
        //3.通过工厂得到jestclient
        JestClient client = factory.getObject();

        //二.中间操作
        //1.使用beans封装json请求
        Employee employee = new Employee("1001", 20, 2000.0, "李三2", "男", "吃饭睡觉");
        //2.建造者建造写入对象
        Index write = new Index.Builder(employee).index("test").type("emps").id("1000001").build();
        //3.执行写入
        DocumentResult result = client.execute(write);
        //4.打印得到的响应码
        System.out.println(result.getResponseCode());

        //关闭客户端
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
